package com.health.viewscontrols.medical;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ServiceUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.BTDevice;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.medical.adapter.MedicalAdapter;
import com.health.viewscontrols.medical.base.BasePtActivity;
import com.health.viewscontrols.medical.base.DeviceEnum;
import com.health.viewscontrols.tjbg.TjbgActivity;
import com.nova.health.ble.BleConfigConstants;
import com.novabluetooth.views.BluetoothDeviceBindActivity2;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

/**
 * 签约体检页面
 */
public class BleMainActivity extends BasePtActivity {

    private ARCHIVE_BASEINFO userinfo = null;
    private ImageView iv_sex, iv_photo, iv_tjbg;
    private TextView jtys_list_name, jtys_list_age, tv_mobile, tv_createtime, jtys_list_address, tv_idcard;
    private RecyclerView rv_medical;
    private MedicalAdapter medicalAdapter;
    private List<DeviceEnum> deviceEnumList = new ArrayList();

    @Override
    public void initData(@Nullable Bundle bundle) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_ble_main;
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        userinfo = NovaApplication.getUserInfo();
        rv_medical = findViewById(R.id.rv_medical);
        iv_sex = findViewById(R.id.iv_sex);
        iv_photo = findViewById(R.id.iv_photo);
        iv_tjbg = findViewById(R.id.iv_tjbg);
        jtys_list_name = findViewById(R.id.jtys_list_name);
        jtys_list_age = findViewById(R.id.jtys_list_age);
        tv_mobile = findViewById(R.id.tv_mobile);
        tv_createtime = findViewById(R.id.tv_createtime);
        jtys_list_address = findViewById(R.id.jtys_list_address);
        tv_idcard = findViewById(R.id.tv_idcard);
        if (userinfo != null) {
            jtys_list_name.setText(userinfo.getCUSTOMERNAME());
            int age = GlobalConts.getAge(userinfo.getBIRTHDAY());
            if (age != -1) {
                jtys_list_age.setText(age + "岁");
            } else {
                jtys_list_age.setText("");
            }
            List<BD_DD_INFO> bdxingbie = FDbManager.getInstance(getApplicationContext())
                    .findAllByWhereInInfoDB(BD_DD_INFO.class, "PARENTID = 'DM01-08'");
            String sex = "";
            for (BD_DD_INFO bdi : bdxingbie) {
                if (userinfo.getSEX().equals(bdi.getID())) {
                    sex = bdi.getDATAINFO();
                }
            }
            tv_idcard.setText("身份证：" + userinfo.getIDCARD());
            if (null != userinfo.getCREATED_DATE() && !userinfo.getCREATED_DATE().equals("")) {
                String happenTime = userinfo.getCREATED_DATE();
                tv_createtime.setText("建档日期：" + happenTime);
            } else {
                tv_createtime.setText("");
            }

            if ("女".equals(sex)) {
                iv_photo.setImageResource(R.drawable.head_youth_y);
                iv_sex.setImageResource(R.drawable.woman);
            } else {
                iv_photo.setImageResource(R.drawable.head_youth_x);
            }
            tv_mobile.setText(userinfo.getPHONE());
            tv_mobile.setText(userinfo.getPHONE());
            jtys_list_address.setText("住址：" + userinfo.getHOUSEHOLD_ADDR());
        }

        iv_tjbg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BleMainActivity.this, TjbgActivity.class));
            }
        });

    }

    @Override
    public void doBusiness() {
        List<DeviceEnum> deviceEnums = DeviceEnum.getConfig();
        //配置过，过滤出只显示得项目
        for (int i = 0; i < deviceEnums.size(); i++) {
            if (deviceEnums.get(i).isShow()) {
                deviceEnumList.add(deviceEnums.get(i));
            }
        }
//        deviceEnumList = DeviceEnum.getDeviceList();
        initAdapter();

        medicalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startDeviceActivity(position);
            }
        });

    }

    private void initAdapter() {
        medicalAdapter = new MedicalAdapter(R.layout.item_medical_list, deviceEnumList, false);
        rv_medical.setLayoutManager(new GridLayoutManager(this, 4));
        rv_medical.setAdapter(medicalAdapter);
    }

    private void startDeviceActivity(int position) {
        DeviceEnum deviceEnum = checkDeviceMac(deviceEnumList.get(position));
        //isBind为true 并且mac为空需要绑定
        if (deviceEnum.getIsBind() && TextUtils.isEmpty(deviceEnum.getMac())) {
            startBind();
        } else {
            if (deviceEnum.getCls() != null) {
                startBle(deviceEnum);
            } else {
                ToastUtils.showShort("开发中");
            }
        }
    }

    private void startBle(DeviceEnum deviceEnum) {
        startActivity(new Intent(BleMainActivity.this, deviceEnum.getCls())
                .putExtra("deviceBean", deviceEnum));
    }

    private void startBind() {
        Intent intent = new Intent(this, BluetoothDeviceBindActivity2.class);
        startActivity(intent);
    }

    private DeviceEnum checkDeviceMac(DeviceEnum deviceEnum) {
//        List<BTDevice> btDevices1 = NovaApplication.fdbmanger.findAllBT(BTDevice.class);
//        for (int i = 0; i < btDevices1.size(); i++) {
//            Log.e(TAG, "checkDeviceMac: " + btDevices1.get(i).getName() + btDevices1.get(i).getMacAddress());
//        }
        List<BTDevice> btDevices = NovaApplication.fdbmanger.findAllBTByWhere(BTDevice.class,
                "name = '" + deviceEnum.getTitle() + "'");
        if (btDevices != null && btDevices.size() > 0) {
            deviceEnum.setMac(btDevices.get(0).getMacAddress());
        } else {
            deviceEnum.setMac("");
        }
        return deviceEnum;
    }

}
